package com.woniu.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.woniu.entity.Floors;
import com.woniu.entity.HouseHoldinfos;
import com.woniu.entity.Households;
import com.woniu.entity.Units;

import com.woniu.mapper.FloorsMapper;
import com.woniu.mapper.HouseHoldinfosMapper;
import com.woniu.mapper.HouseholdsMapper;
import com.woniu.mapper.UnitsMapper;
import com.woniu.service.IUnitsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Gapper
 * @since 2021-03-29
 */
@Service
public class UnitsServiceImpl extends ServiceImpl<UnitsMapper, Units> implements IUnitsService {

    @Autowired
    private UnitsMapper unitMapper;

    @Autowired
    private FloorsMapper floorsMapper;

    @Autowired
    private HouseholdsMapper holdsMapper;

    @Autowired
    private HouseHoldinfosMapper houseHoldinfosMapper;

    /**
     * 查询全部
     */
    @Override
    public List<Units> findAll() {
        QueryWrapper<Units> queryWrapper = new QueryWrapper<>();
        List<Units> list = unitMapper.find(queryWrapper);


        return list;
    }

    /**
     * 查询一个
     */
    @Override
    public List<Units> findById(String uid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("Unit_ID", uid);
        List<Units> list = unitMapper.find(queryWrapper);

        return list;
    }

    /**
     * 删除单元的同时删除，楼层，户型，户型详情表
     */
    @Override
    @Transactional
    public int deleteById(String uid) {
        //fid
        QueryWrapper fquery = new QueryWrapper();
        fquery.in("Unit_ID", uid);
        List<Floors> flist = floorsMapper.selectList(fquery);
        //将fid放入集合中
        Set fid = new HashSet();
        for (int i = 0; i < flist.size(); i++) {
            fid.add(flist.get(i).getFloorId());
        }

        //hid
        QueryWrapper hquery = new QueryWrapper();
        hquery.in("Floor_ID", fid);
        List<Households> hlist = holdsMapper.selectList(hquery);
        //将hid放入集合当中
        Set hid = new HashSet();
        for (int i = 0; i < hlist.size(); i++) {
            hid.add(hlist.get(i).getHouseholdId());
        }

        //hfid
        QueryWrapper hfquery = new QueryWrapper();
        hfquery.in("HouseHold_ID", hid);
        List<HouseHoldinfos> hflist = houseHoldinfosMapper.selectList(hfquery);
        //将hfid放入集合
        Set hfid = new HashSet();
        for (int i = 0; i < hflist.size(); i++) {
            hfid.add(hflist.get(i).getHouseholdinfoId());
        }
        int rows = 0;
        //删除户型详情
        if (hfid.size() > 0) {
            rows = houseHoldinfosMapper.deleteBatchIds(hfid);
        }

        //删除户型
        if (hid.size() > 0) {
            rows = holdsMapper.deleteBatchIds(hid);
        }

        //删除楼层
        if (fid.size() > 0) {
            rows = floorsMapper.deleteBatchIds(fid);
        }

        //删除单元
        rows = unitMapper.deleteBatchIds(Arrays.asList(uid));

        return rows;
    }

    /**
     根据楼号查询单元
     */

    @Override
    public List<Units> findByBid(String bid) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("Building_ID",bid);
        List<Units> list = unitMapper.selectList(queryWrapper);
        return list;
    }


    /**
     * 根据单元名去重
     * */
    @Override
    public List<Units> find() {
        QueryWrapper<Units> queryWrapper = new QueryWrapper<>();
        queryWrapper.groupBy("Unit_Name");
        List<Units> list = unitMapper.find(queryWrapper);
        return list;
    }




}
